(function($) {
    var EventsList = function(element, options) {
        var $main = $('#wrapper');
        var $list = $main.find('#orders-list-bd');
        var $pullDown = $main.find('#pull-down');
        var $pullUp = $main.find('#pull-up');
        var topOffset = -$pullDown.outerHeight();

        this.prev = this.next = this.start = options.params.start;
        this.total = null;

        this.getURL = function(params) {
            var queries = [];
            for (var key in  params) {
                if (key !== 'start') {
                    queries.push(key + '=' + params[key]);
                }
            }
            queries.push('start=');
            return options.api + '?' + queries.join('&');
        };


        this.renderList = function(start, type) {
            var _this = this;
            var $el = $pullDown;
            if (type == 'refresh') {
                loading();
            }
            $.getJSON(this.URL + start).then(function(data) {
                //closeLoading();
                _this.total = data.total;

                if (type === 'refresh') {
                    closeLoading();
                    $list.html(data.html);
                } else{
                    $list.append(data.html);
                }
                if(data.total <= 10){
                    _this.hidePullDown();
                }else{
                    _this.showPullDown();
                }

                // refresh iScroll
                setTimeout(function() {
                    _this.iScroll.refresh();
                }, 100);
            }, function() {
                console.log('Error...')
            }).always(function() {
                if (type !== 'refresh') {
                    //_this.setLoading();
                    //_this.showPullDown();
                }else{
                    //loading();
                }
            });
        };

        //没有数据 隐藏下拉刷新
        this.hidePullDown = function() {
            $('#pull-down').hide();
            $('#drop-down').hide();
        };

        //有数据 显示下拉刷新
        this.showPullDown = function() {
            $('#pull-down').show();
            $('#drop-down').show();
        };

        //加载中
        this.setLoading = function() {
            $('#pull-down').show();
            $('#drop-down').hide();
            $('#drop-down-load').show();
            setTimeout(function () {
                $('#drop-down-load').hide();
            }, 1000);
        };

        //没有了  最后一页
        this.lastPage = function(){
            $('#pull-down').show();
            $('#drop-down').hide();
            $('#last-page').show();
            setTimeout(function () {
                //$('#drop-down').show();
                //$('#last-page').hide();
            }, 1000);
        }

        this.init = function() {
            var myScroll = this.iScroll = new $.AMUI.iScroll('#wrapper', {
                click:true,
                //disableTouch: true,
                probeType: 3,
                mouseWheel: true
            });
            // myScroll.scrollTo(0, topOffset);
            var _this = this;
            var pullFormTop = false;
            var pullStart;

            this.URL = this.getURL(options.params);
            this.renderList(options.params.start, 'refresh');

            myScroll.on('scrollStart', function() {
                if (this.y >= topOffset) {
                    pullFormTop = true;
                }

                pullStart = this.y;
                // console.log(this);
            });

            myScroll.on('scrollEnd', function() {
                if (pullFormTop && this.directionY === -1) {
                    //_this.handlePullDown();
                }
                pullFormTop = false;

                // pull up to load more
                if (pullStart === this.y && (this.directionY === 1)) {
                    _this.handlePullUp();
                }
            });
        };

        //上拉
        this.handlePullDown = function() {
            console.log('handle pull down');
            if (this.prev > 0) {
                this.prev -= options.params.count;
                this.renderList(this.prev, 'refresh');
            } else {
                console.log('别刷了，没有了');
            }
        };

        //下拉刷新
        this.handlePullUp = function() {
            this.next += options.params.count;
            if (this.next < this.total) {
                this.setLoading();
                this.renderList(this.next, 'load');
            } else {
                //最后一页
                this.lastPage();
            }
        };

        this.orderList = function(){
            _othis = this;
            $('.scy-object').on(status, '.order-list-nav', function(){
                if(!$(this).hasClass('active')){
                    var status = $(this).attr('data-status');
                    options.params.status = status;
                    _othis.URL = _othis.getURL(options.params);
                    $(this).siblings().removeClass('active');
                    $(this).addClass('active');
                    _othis.renderList(0, 'refresh');
                }
            });
        };

        this.goodsReview = function(){
            _othis = this;
            $('.scy-option').on(status, '.order-list-nav', function(){
                if(!$(this).hasClass('aactive')){
                    var score = $(this).attr('data-score');
                    $(this).siblings().removeClass('active');
                    $(this).addClass('active');
                    _othis.renderList(0+'&reviewScore='+score, 'refresh');
                }
            });
        };
    };

    $(function() {
        //全部订单
        if(location.pathname.indexOf('tourism/user/orderList') >= 0){
            var status = getQueryString('status');
            var app = new EventsList(null, {
                api: hostUrl()+'user/ajaxOrderList',
                params: {
                    start: 0,
                    count: 30,
                    status: status != null ? status : 0
                }
            });
            app.init();
            app.orderList();
        }else if(location.pathname.indexOf('tourism/user/orderSearch') >= 0){
            var keyword = GetRequest('keyword');
            var app = new EventsList(null, {
                api: hostUrl()+'user/ajaxOrderList',
                params: {
                    start: 0,
                    count: 30,
                    keyword:keyword
                }
            });
            app.init();
            app.orderList();
        } else if(location.pathname.indexOf('tourism/goods/reviews') >= 0){
            var id = GetRequest('goodsAttrId');
            var app = new EventsList(null, {
                api: hostUrl()+'goods/ajaxReview',
                params: {
                    start: 0,
                    count: 30,
                    goodsAttrId:id
                }
            });
            app.init();
            app.goodsReview();
        }
    });

    document.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, false);
})(window.jQuery);